package com.cheng.dreams.greatlydreams.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;

import com.cheng.dreams.greatlydreams.R;

/**
 * Created by cheng on 2017/3/15.
 * ━━━━━━神兽出没━━━━━━
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　　　　　┃
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛Code is far away from bug with the animal protecting
 * 　　　　┃　　　┃    神兽保佑,代码无bug
 * 　　　　┃　　　┃
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * <p>
 * ━━━━━━感觉萌萌哒━━━━━━
 */

public class SuperCircle extends View{

    private Display display=null;
    //屏幕的宽
    private int screemWidth=0;
    //屏幕的高
    private int screemHeight=0;

    int color_white=Color.WHITE;
    int color_black=Color.BLACK;

    Paint p=null;
    Paint pw=null;
    Paint pb=null;

    int radius;

    int degress=0;
    int time=1000;

    public SuperCircle(Context context) {
        this(context,null);
    }

    public SuperCircle(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public SuperCircle(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        display=((WindowManager)getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        screemWidth=display.getWidth();
        screemHeight=display.getHeight();
        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.superC,defStyleAttr,0);
        color_white = typedArray.getColor(R.styleable.superC_white_color, Color.WHITE);
        color_black = typedArray.getColor(R.styleable.superC_black_color, Color.BLACK);
        typedArray.recycle();
        initCanvas();
    }

    public void initCanvas() {
        p=new Paint();
        p.setColor(Color.BLUE);
        p.setAntiAlias(true);
        p.setStrokeWidth(3);
        p.setStyle(Paint.Style.STROKE);

        pw=new Paint();
        pw.setAntiAlias(true);
        pw.setDither(true);
        pw.setColor(color_white);
        pw.setStrokeWidth(5);
        pw.setStyle(Paint.Style.FILL);

        pb=new Paint();
        pb.setAntiAlias(true);
        pb.setDither(true);
        pb.setColor(color_black);
        pb.setStrokeWidth(5);
        pb.setStyle(Paint.Style.FILL);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthModel=MeasureSpec.getMode(widthMeasureSpec);
        int heightModel=MeasureSpec.getMode(heightMeasureSpec);
        int widthSize=MeasureSpec.getSize(widthMeasureSpec);
        int heightSize=MeasureSpec.getSize(heightMeasureSpec);
        int width;
        int height;
        if(widthModel==MeasureSpec.EXACTLY){
            width=widthSize;
        }else{
            width=screemWidth;
        }
        if(heightModel==MeasureSpec.EXACTLY){
            height=heightSize;
        }else{
            height=screemHeight/3;
        }
        setMeasuredDimension(width,height);
    }

    @Override
    protected void onDraw(final Canvas canvas) {
        //得到圆的半径
        if(getWidth()>getHeight()){
            radius=getHeight()/2;
        }else{
            radius=getWidth()/2;
        }

        canvas.translate(getWidth()/2,getHeight()/2);
        canvas.rotate(degress);
        canvas.drawCircle(0,0,radius,p);

        RectF rectF = new RectF(-radius,-radius,radius,radius);
        canvas.drawArc(rectF,270,180,true,pw);
        canvas.drawArc(rectF,270,-180,true,pb);

        canvas.drawCircle(0,-radius/2,radius/2,pb);
        canvas.drawCircle(0,radius/2,radius/2,pw);

        canvas.drawCircle(0,-radius/2,radius/8,pw);
        canvas.drawCircle(0,radius/2,radius/8,pb);
    }

    public void startRotate(int time){
        this.time=time;
        handler.sendEmptyMessageDelayed(0, time);

    }

    public void stoptRotate(){
        handler.sendEmptyMessageDelayed(1, time);
    }

    final Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==0){
                degress+=6;
                invalidate();
                this.sendEmptyMessageDelayed(0, time);
            }
        }
    };
}
